<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.7.6/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="status" lay-verify="" lay-search="">
                                    <option value=""></option>
                                    <option value="0">禁用</option>
                                    <option value="1">启用</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">年份</label>
                            <div class="layui-input-inline">
                                <select name="year" id="year"  lay-search="">
                                    <option value=""></option>
                                    <option value="2022">2022</option>
                                    <option value="2023">2023</option>
                                    <option value="2024">2024</option>
                                    <option value="2025">2025</option>
                                    <option value="2026">2026</option>
                                    <option value="2027">2027</option>
                                    <option value="2028">2028</option>
                                    <option value="2029">2029</option>
                                    <option value="2030">2030</option>
                                    <option value="2031">2031</option>
                                    <option value="2032">2032</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">分类</label>
                            <div class="layui-input-inline">
                                <select name="holiday_type" id="holiday_type" lay-verify="" lay-search="">
                                    <option value=""></option>
                                    <option value="0">其他</option>
                                    <option value="1">情人节</option>
                                    <option value="2">七夕</option>
                                    <option value="3">520</option>
                                    <option value="4">妇女节</option>
                                    <option value="5">教师节</option>
                                    <option value="6">母亲节</option>
                                    <option value="7">圣诞节</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn  layui-btn-warm layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-edit-btn" lay-event="edit"> 编辑 </button>
<!--                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>-->
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script type="text/html" id="switchTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="statusDemo" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script src="/static/lib/layui-v2.7.6/layui.js" charset="utf-8"></script>
<script src="/static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','soulTable'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            soulTable = layui.soulTable;
        table.render({
            elem: '#currentTableId',
            url: '/huashu/index',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox"},
                {field: 'serial_num', title: '编号',sort: true,align:'center',edit: 'text'},
                {field: 'img',  title: '图片', align:'center', style:'height:80px',templet:function (d) {
                        return '<img  src="/img'+ (d.img.file_path == "/" ? "":d.img.file_path) + '/' +d.img.file_name +'" onclick="showimg(this)" id="driimg" style="display:inline-block;width: 40px;height: 70px;"/>';
                    }},
                {field: 'name', title: '名称',width:'40%',align:'center',edit: 'text'},
                {field: 'status', title:'状态', sort: true,templet: '#switchTpl', unresize: true},
                {field: 'price',  title: '价格',sort: true,align:'center',edit: 'text'},
                {title: '操作', toolbar: '#currentTableBar', align: "center",fixed:'right'}
            ]],
            done: function () {    // soulTable专用重渲染
                soulTable.render(this)

            },
            // autoColumnWidth: {  //自适应列宽
            //     init: true
            // },
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            // even: false, //开启隔行背景
            // size: 'sm' //小尺寸的表格
        });

        //渲染下拉
        $.ajax({
            url: "/config/getConfigByKey",
            type: 'post',
            data:{"key":"holiday_type"},
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            async: false,
            success: function (ret) {
                var date = new Date() // 获取时间
                var year = date.getFullYear() // 获取年
                $("#year").val(year)
                $("#holiday_type").val(ret.data)
                form.render('select');
                // layui.form.render('select')
            },
            error: function (xhr, textstatus, thrown) {
                layer.msg('操作失败');
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }

            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加花束',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/static/page/huashu/add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'edit') {
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var index = layer.open({
                    title: '编辑花束',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/static/page/huashu/edit.html?id='+data[0].id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data.id)
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑花束',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/static/page/huashu/edit.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'changeSendTime'){
                layer.prompt({
                    formType: 2
                    ,title: '修改 ID 为 ['+ data.id +'] 的用户签名'
                    ,value: data.send_time
                }, function(value, index){
                    layer.close(index);

                    //这里一般是发送修改的Ajax请求

                    //同步更新表格和缓存对应的值
                    obj.update({
                        send_time: value
                    });
                });
            }
        });
        //监听单元格编辑
        table.on('edit(currentTableFilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            // layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
            $.ajax({
                url: "/huashu/edit",
                type: 'post',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                data: JSON.stringify(data),
                async: false,
                success: function (ret) {
                    // var msg = ret.msg || "操作成功"
                    layer.msg("操作成功");
                },
                error: function (xhr, textstatus, thrown) {
                    layer.msg('操作失败');
                }
            });
        });
        //监听状态操作
        form.on('switch(statusDemo)', function(obj){
            // layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
            var id = obj.value
            var status = obj.value
            if (obj.elem.checked) {
                status = 1
            } else {
                status = 0
            }
            $.ajax({
                url: "/huashu/edit",
                type: 'post',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                data: JSON.stringify({id:parseInt(id),status:status}),
                async: false,
                success: function (ret) {
                    // var msg = ret.msg || "操作成功"
                    layer.msg("操作成功");
                },
                error: function (xhr, textstatus, thrown) {
                    layer.msg('操作失败');
                }
            });
        });
    });
    function showimg(t){
        layer.open({
            type:1,
            title:false,
            closeBtn:0,
            area:'400px',
            skin:'layui-layer-nobg',
            shadeClose:true,
            content:'<img style="display: inline-block;width: 100%;height: 100%;" src="'+t.src+'">'
        });
    }
</script>

</body>
</html>